package com.tsvetanv.data.binding.property.editors.supported;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import junit.framework.TestCase;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.ServletRequestDataBinder;

import com.tsvetanv.data.binding.model.property.editors.supported.DateCommandBean;

public class DateCommandBeanBindTest extends TestCase {
	DateCommandBean bean;
	ServletRequestDataBinder binder;
	MockHttpServletRequest request;

	@Override
	protected void setUp() throws Exception {
		bean = new DateCommandBean();
		binder = new ServletRequestDataBinder(bean, "beanName");
		request = new MockHttpServletRequest();
	}

	public void testDateCommandBeanBind() throws Exception {

		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		Date expectedDate = dateFormat.parse("20-11-2011");

		CustomDateEditor dateEditor = new CustomDateEditor(dateFormat, true);
		binder.registerCustomEditor(Date.class, dateEditor);

		request.addParameter("date", "20-11-2011");

		binder.bind(request);
		// System.out.println(bean.getDate());
		assertEquals(expectedDate, bean.getDate());
	}

}
